package com.example.demo.controller;

import com.example.demo.entity.Shift;
import com.example.demo.dao.ShiftDao;
import com.example.demo.service.Impl.ShiftServiceImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Project:demo
 * @Description: 排班控制层
 * @Author: Mr.Hunter
 * @Date:2023/3/25/18:47
 */
@RestController
@RequestMapping("/Shift")
public class ShiftController {

    @Autowired
    private ShiftServiceImpl shiftService;

    @Autowired
    private ShiftDao shiftDao;


    @ApiOperation("[×]通过店铺ip和日期查询排班表")
    @ResponseBody
    @GetMapping(value = "selectByIdAndDate/{store_id}/{date}")
    public List<Shift> selectByStoreIdByWorkDate(@PathVariable("store_id")Integer store_id, @PathVariable("date")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")@DateTimeFormat(pattern = "yyyy-MM-dd")String date) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = format.parse(date);
        java.sql.Date work_date = new java.sql.Date(workDate.getTime());
        return shiftService.findByStoreIdAndWorkDate(store_id, work_date);
    }


    @ApiOperation("[×]店铺ip和日期查询排班表【x】")
    @ResponseBody
    @GetMapping(value = "findAllByStoreIdAndWorkDate/{store_id}/{work_date}")
    public List<Shift> findAllByStoreIdAndWorkDate(@PathVariable("store_id")Integer store_id,@PathVariable("work_date")
    @DateTimeFormat(pattern = "yyyy-MM-dd")String date) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = format.parse(date);
        java.sql.Date work_date = new java.sql.Date(workDate.getTime());
        return shiftService.findAllByStoreIdAndWorkDate(store_id,work_date);
    }



    @ApiOperation("[×]单个员工工作表修改")
    @ResponseBody
    @PostMapping(path = "/updateStaffScheduling/{staff_id}/{store_id}/{staff_scheduling}/{work_date}")
    public String updateStaffScheduling(@PathVariable("staff_id")Integer staff_id,@PathVariable("store_id")Integer store_id
    ,@PathVariable("staff_scheduling")String staff_scheduling,@PathVariable("work_date") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                            String date) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = format.parse(date);
        java.sql.Date work_date = new java.sql.Date(workDate.getTime());
        int i = shiftService.updateStaffScheduling(staff_id, store_id, staff_scheduling, work_date);
        if (i != 0)return "修改成功";
        return "未找到相关数据";
    }

    @ApiOperation("[×]通过开始下标还有结束下标进行员工上班表的修改")
    @ResponseBody
    @PostMapping(path = "/updateStaffScheduling/{staff_id}/{store_id}/{begin_time}/{end_time}/{work_date}")
    public String updateStaffSchedulingByBeginAndEnd(@PathVariable("staff_id")Integer staff_id,@PathVariable("store_id")Integer store_id
            ,@PathVariable("begin_time")Integer  begin_time,@PathVariable("end_time")Integer end_time
            ,@PathVariable("work_date") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                        String date) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = format.parse(date);
        java.sql.Date work_date = new java.sql.Date(workDate.getTime());
        int i = shiftService.updateKeepNowStaffSchedulingByBeginAndEnd(staff_id, store_id, begin_time, end_time, work_date);
        if (i != 0)return "修改成功";
        return "未找到相关数据";
    }
    @ApiOperation("[×]通过开始下标还有结束下标进行员工上班表的删除")
    @ResponseBody
    @PostMapping(path = "/deleteStaffScheduling/{staff_id}/{store_id}/{work_date}")
    public String deleteStaffSchedulingByBeginAndEnd(@PathVariable("staff_id")Integer staff_id,@PathVariable("store_id")Integer store_id
            ,@PathVariable("work_date") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                                         String date) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = format.parse(date);
        java.sql.Date work_date = new java.sql.Date(workDate.getTime());
        int i = shiftService.deleteStaffSchedulingByAllZone(staff_id, store_id, work_date);
        if (i != 0)return "修改成功";
        return "未找到相关数据";
    }

    @ApiOperation("[×]保持当前员工班别不动的前提下，进行当前员工的班次的增加")
    @ResponseBody
    @PostMapping(path = "/updateKeepNowStaffSchedulingByBeginAndEnd/{staff_id}/{store_id}/{work_date}/{begin_time}/{end_time}")
    public String updateKeepNowStaffSchedulingByBeginAndEnd(@PathVariable("staff_id")Integer staff_id,@PathVariable("store_id")Integer store_id
            ,@PathVariable("begin_time")Integer  begin_time,@PathVariable("end_time")Integer end_time
            ,@PathVariable("work_date") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                                     String date) throws ParseException {
        java.sql.Date work_date = dateConversion(date);
        int i = shiftService.updateKeepNowStaffSchedulingByBeginAndEnd(staff_id, store_id, begin_time, end_time, work_date);
        if (i != 0)return "修改成功";
        return "未找到相关数据";
    }

    @ApiOperation("[×]将当前员工班次置空，再进行当前员工的班次的增加")
    @ResponseBody
    @PostMapping(path = "/updateEmptyStaffSchedulingByBeginAndEnd/{staff_id}/{store_id}/{work_date}/{begin_time}/{end_time}")
    public String updateEmptyStaffSchedulingByBeginAndEnd(@PathVariable("staff_id")Integer staff_id,@PathVariable("store_id")Integer store_id
            ,@PathVariable("begin_time")Integer  begin_time,@PathVariable("end_time")Integer end_time
            ,@PathVariable("work_date") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                                            String date) throws ParseException {
        java.sql.Date work_date = dateConversion(date);
        int i = shiftService.updateEmptyStaffSchedulingByBeginAndEnd(staff_id, store_id, begin_time, end_time, work_date);
        if (i != 0)return "修改成功";
        return "未找到相关数据";
    }

    @ApiOperation("[×]将当前员工某一区间的班次进行调整")
    @ResponseBody
    @GetMapping(path = "/updateEmptyStaffSchedulingByBeginAndEnd/{staff_id}/{store_id}/{work_date}/" +
            "{begin_time}/{end_time}/{origin_start_time}/{origin_end_time}")
    public String updateRangeStaffSchedulingByBeginAndEnd(@PathVariable("staff_id")Integer staff_id,@PathVariable("store_id")Integer store_id
            ,@PathVariable("begin_time")Integer  begin_time,@PathVariable("end_time")Integer end_time
            ,@PathVariable("work_date") @DateTimeFormat(pattern = "yyyy-MM-dd") String date,@PathVariable("origin_start_time")Integer origin_start_time,
                                                              @PathVariable("origin_end_time")Integer origin_end_time) throws ParseException {
        java.sql.Date work_date = dateConversion(date);
        int i = shiftService.updateRangeStaffSchedulingByBeginAndEnd(staff_id, store_id, begin_time, end_time, work_date,origin_start_time,origin_end_time);
        if (i != 0)return "修改成功";
        return "未找到相关数据";
    }

    @ApiOperation("[×]根据开始与结束时间段请假，进行当前员工班次调整")
    @ResponseBody
    @GetMapping(path = "/deleteByBeginAndEnd/{staff_id}/{store_id}/{work_date}/{begin_time}/{end_time}")
    public String updateStaffSchedulingByTimeSlot(@PathVariable("staff_id")Integer staff_id,@PathVariable("store_id")Integer store_id
            ,@PathVariable("begin_time")Integer  begin_time,@PathVariable("end_time")Integer end_time
            ,@PathVariable("work_date") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                                      String date) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = format.parse(date);
        java.sql.Date work_date = new java.sql.Date(workDate.getTime());
        int i = shiftService.updateKeepNowStaffSchedulingByBeginAndEnd(staff_id, store_id, begin_time, end_time, work_date);
        if (i != 0)return "修改成功";
        return "未找到相关数据";
    }
    private static java.sql.Date dateConversion(String date) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = format.parse(date);
        java.sql.Date work_date = new java.sql.Date(workDate.getTime());
        return  work_date;
    }

}